package com.clas.liu.myapplication.view.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * Created by liu on 2017/8/8.
 */

public class ItemImageView extends android.support.v7.widget.AppCompatImageView {
	public boolean isLongImage = false;
	public ItemImageView(Context context) {
		super(context);
	}

	public ItemImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public ItemImageView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		Drawable d = this.getDrawable();
			int mWidth = d.getIntrinsicWidth();
			int mHeight = d.getIntrinsicHeight();
		float x = (float) mHeight / mWidth;
			if (mHeight > 2 * mWidth) {
				mWidth = 3 * mWidth;
				mHeight = mWidth;
				this.isLongImage = true;
				setMeasuredDimension(mWidth, mHeight);
				this.setScaleType(ScaleType.CENTER_CROP);
			} else {
				if (mHeight > mWidth)
				{
					//mWidth = (int)(MeasureSpec.getSize(widthMeasureSpec)/2);
					//mHeight = (int) (mWidth * x);
					setMeasuredDimension(mWidth, mHeight);
				} else if (mWidth < 200 && mHeight < 200) {
					mWidth = 2 * mWidth;
					mHeight = 2 * mHeight;
					setMeasuredDimension(mWidth, mHeight);
					this.setScaleType(ScaleType.CENTER_CROP);
				} else if (mWidth > mHeight) {
					mWidth = MeasureSpec.getSize(widthMeasureSpec);
					mHeight = (int) (mWidth * x);
					//MeasureSpec.makeMeasureSpec(mHeight,MeasureSpec.getMode(heightMeasureSpec));
					setMeasuredDimension(mWidth, mHeight);
				} else
					setMeasuredDimension(mWidth, mHeight);
			}
		}
}
